﻿using Microsoft.Phone.Controls;
using System;
using System.Windows;
using System.Windows.Navigation;
using Windows.Networking.Proximity;

namespace InTheHand.Phone.Tasks
{
    internal partial class ShareNfcLinkTaskPage : PhoneApplicationPage
    {
        private ProximityDevice device;
        private long id;

        public ShareNfcLinkTaskPage()
        {
            InitializeComponent();
            
            Visibility light = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];
            string imagename = (light == System.Windows.Visibility.Visible) ? "share-light" : "share";
            System.Windows.Resources.StreamResourceInfo sri = System.Windows.Application.GetResourceStream(new Uri(string.Format("/InTheHand.Phone.Nfc;component/{0}.png", imagename), UriKind.Relative));
            System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
            image.SetSource(sri.Stream);
            ShareImage.Source = image;

            // set layout for RTL languages
            switch (System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
            {
                case "ar":
                case "he":
                    this.FlowDirection = System.Windows.FlowDirection.RightToLeft;
                    break;

                default:
                    this.FlowDirection = System.Windows.FlowDirection.LeftToRight;
                    break;
            }

            // apply localised resources
            this.TapAndSendTitle.Text = InTheHand.Phone.Resources.Resources.TapAndSend;
            this.TapAndSendDescription.Text = InTheHand.Phone.Resources.Resources.TapAndSendDescription;

            // on 1080p devices this text size is reduced (actual factor is not documented so based on best guess)
            if(InTheHand.Graphics.Display.DisplayInformation.GetForCurrentView().ResolutionScale == Graphics.Display.ResolutionScale.Scale225Percent)
            {
                this.TapAndSendDescription.FontSize = this.TapAndSendDescription.FontSize / 1.25;
            }

            this.CancelButton.Content = InTheHand.Phone.Resources.Resources.Cancel;

            try
            {
                device = ProximityDevice.GetDefault();
            }
            catch
            {
                throw new InvalidOperationException("NFC not supported");
            }

            if (device == null)
            {
                throw new InvalidOperationException("NFC not supported");
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        void MessageTransmitted(ProximityDevice device, long id)
        {
            if (id == this.id)
            {
                Dispatcher.BeginInvoke(NavigationService.GoBack);
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Uri LinkUri = new Uri(NavigationContext.QueryString["uri"]);
            if (device != null)
            {
                id = device.PublishUriMessage(LinkUri, MessageTransmitted);
            }

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            if (id != 0)
            {
                device.StopPublishingMessage(id);
                id = 0;
            }

            base.OnNavigatingFrom(e);
        }
    }
}